package com.ruoyi.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.app.domain.AppHtPoints;
import com.ruoyi.app.domain.AppUser;
import com.ruoyi.app.mapper.AppHtPointsMapper;
import com.ruoyi.app.mapper.AppUserMapper;
import com.ruoyi.app.service.IAppHtPointsService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 积分记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-09
 */
@Service
public class AppHtPointsServiceImpl extends ServiceImpl<AppHtPointsMapper, AppHtPoints> implements IAppHtPointsService {
    @Autowired
    private AppHtPointsMapper appHtPointsMapper;
    @Autowired
    private AppUserMapper appUserMapper;

    /**
     * 查询积分记录
     *
     * @param id 积分记录主键
     * @return 积分记录
     */
    @Override
    public AppHtPoints selectAppHtPointsById(Integer id) {
        return appHtPointsMapper.selectAppHtPointsById(id);
    }

    /**
     * 查询积分记录列表
     *
     * @param appHtPoints 积分记录
     * @return 积分记录
     */
    @Override
    public List<AppHtPoints> selectAppHtPointsList(AppHtPoints appHtPoints) {
        return appHtPointsMapper.selectAppHtPointsList(appHtPoints);
    }

    /**
     * 新增积分记录
     *
     * @param appHtPoints 积分记录
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertAppHtPoints(AppHtPoints appHtPoints) {
        // 查询这个是收入还是支出
        AppUser appUser = appUserMapper.selectAppUserById(Long.valueOf(appHtPoints.getUserId()));
        int num= 0;
        if (appHtPoints.getType() == 1) {
            // 收入
            num = appUser.getPoints() + appHtPoints.getNum();
            appUser.setPoints(num);
        }else {
            // 判断积分是否足够
            if(appUser.getPoints() < appHtPoints.getNum()) {
                return AjaxResult.error("积分不足");
            }
            num = appUser.getPoints() - appHtPoints.getNum();
            appUser.setPoints(num);
        }
        appUserMapper.updateAppUser(appUser);
        appHtPoints.setRemainingPoints(num);
        appHtPoints.setCreateTime(DateUtils.getNowDate());
        return AjaxResult.success(appHtPointsMapper.insertAppHtPoints(appHtPoints));
    }

    /**
     * 修改积分记录
     *
     * @param appHtPoints 积分记录
     * @return 结果
     */
    @Override
    public int updateAppHtPoints(AppHtPoints appHtPoints) {
        appHtPoints.setUpdateTime(DateUtils.getNowDate());
        return appHtPointsMapper.updateAppHtPoints(appHtPoints);
    }

    /**
     * 批量删除积分记录
     *
     * @param ids 需要删除的积分记录主键
     * @return 结果
     */
    @Override
    public int deleteAppHtPointsByIds(Integer[] ids) {
        return appHtPointsMapper.deleteAppHtPointsByIds(ids);
    }

    /**
     * 删除积分记录信息
     *
     * @param id 积分记录主键
     * @return 结果
     */
    @Override
    public int deleteAppHtPointsById(Integer id) {
        return appHtPointsMapper.deleteAppHtPointsById(id);
    }
}

